#-*- coding:gbk -*-
'''
Created on 2012-7-20

@author: xxy
'''
import socket
import enum

class BaseSocket(object):
    def __init__(self, socket_mgr):
        self.m_socket_mgr = socket_mgr
        self.m_socket_mgr.addSocketSet(self)
        self.m_socket = None
        self.m_handle_dict = {enum.SELECT_TYPE.READ:self.handleRead,
                              enum.SELECT_TYPE.WRITE:self.handleWrite,
                              enum.SELECT_TYPE.ERROR:self.handleError}
        
    def fileno(self):
        if self.m_socket == None:
            return 0
        try:
            return self.m_socket.fileno()
        except socket.error:
            self.m_objSock = None 
            raise socket.error
        
    def update(self, type_no, diff):
        #print 'update ' + str(type_no)
        self.m_handle_dict[type_no](diff)
              
    def handleRead(self, diff):
        pass
    
    def handleWrite(self, diff):
        pass
    
    def handleError(self, diff):
        self.close()
        
    def close(self):
        self.m_socket_mgr.delSocket(self)
        self.m_socket.close()